package com.gzyj.server.factory.jtt808.up;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.VehicleIncrement;
import com.gzyj.jtt808.v2013.HeaderMsgBodyProperty;
import com.gzyj.jtt808.v2013.Package;
import com.gzyj.jtt808.v2013.enums.BusinessDataType;
import com.gzyj.jtt808.v2013.enums.JT808TerminalResult;
import com.gzyj.jtt808.v2013.protocol.JTT808_0x8001;
import com.gzyj.jtt808.v2013.protocol.jtt1078.JTT808_0x1206;
import com.gzyj.pojo.gps.GzyjVideoHistory;
import com.gzyj.server.config.LogConfig;
import com.gzyj.server.factory.CommonHandler;
import com.gzyj.server.mapper.GzyjVideoHistoryMapper;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class JTT808_0x1206_Handler implements CommonHandler {

    @Autowired
    private GzyjVideoHistoryMapper gzyjVideoHistoryMapper;

    @Resource(name = "clusterRedisTemplate")
    private RedisTemplate<String, Object> clusterRedisTemplate;
    @Value("${redis.key.vehicle.cmdrs}")
    private String VehicleZdCMDRS;

    @Override
    public void handler(ChannelHandlerContext ctx, Package msg) throws Exception {
        JTT808_0x1206 jtt808_0x1206 = (JTT808_0x1206) msg;


        JTT808_0x8001 jtt808_0x8001 = new JTT808_0x8001();
        HeaderMsgBodyProperty headerMsgBodyProperty = new HeaderMsgBodyProperty();
        headerMsgBodyProperty.setSubcontract(jtt808_0x1206.getPackageHeader().getMsgBodyProperty().isSubcontract());
        headerMsgBodyProperty.setEncryptMethod(jtt808_0x1206.getPackageHeader().getMsgBodyProperty().getEncryptMethod());
        jtt808_0x8001.getPackageHeader().setMsgBodyProperty(headerMsgBodyProperty);
        jtt808_0x8001.getPackageHeader().setTerminalPhoneNo(jtt808_0x1206.getPackageHeader().getTerminalPhoneNo());

        jtt808_0x8001.setMsgNum(jtt808_0x1206.getPackageHeader().getMsgNum());
        jtt808_0x8001.setAckMsgId(jtt808_0x1206.getPackageHeader().getMsgId());
        jtt808_0x8001.setResult(JT808TerminalResult.成功_确认);

        ctx.writeAndFlush(jtt808_0x8001.encode());
        ctx.flush();

        GzyjVideoHistory videoHistory= gzyjVideoHistoryMapper.selectOne(new QueryWrapper<GzyjVideoHistory>().eq("serialNum",jtt808_0x1206.getMsgNum()));
        if(videoHistory!=null){
            videoHistory.setStatus(jtt808_0x1206.getResult().intValue());
            gzyjVideoHistoryMapper.insertOrUpdate(videoHistory);
        }
        clusterRedisTemplate.opsForValue().set(VehicleZdCMDRS + jtt808_0x1206.getPackageHeader().getTerminalPhoneNo() + ":"+0x1206+":" + jtt808_0x1206.getMsgNum(), jtt808_0x1206,10, TimeUnit.MINUTES);


        LogConfig.commondLog.info("808 2013版 {} 指令 解析完成", BusinessDataType.find(jtt808_0x1206.getPackageHeader().getMsgId()));
    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2019.Package msg) throws Exception {

    }

    @Override
    public void handler(ChannelHandlerContext ctx, com.gzyj.jtt808.v2011.Package msg) {

    }

    @Override
    public void handler(VehicleIncrement vehicleInfo, String msg) {

    }
}
